Convert Dictionary to JSON in Python Tutorial Example

您所在的位置:网站首页 python json dump file Convert Dictionary to JSON in Python Tutorial Example

Convert Dictionary to JSON in Python Tutorial Example

2023-02-21 16:49| 来源: 网络整理| 查看: 265

Convert Dictionary to JSON in Python Tutorial Example

Feb 16, 2023

.

Admin

Convert Dictionary to JSON in Python Tutorial Example

Hi Dev,

This article is focused on convert dictionary to json in python tutorial example. you can understand a concept of how to convert dictionary to json in python. Here you will learn how to convert dictionary to json string in python. This post will give you simple example of how to convert dictionary to json file in python. Alright, let鈥檚 dive into the steps.

There are several ways to convert python dictionary to json. we will use dumps() function of json library to convert dictionary to json string in python.

Example 1:

main.py

# Created New Dictionary

myDictionary = {

"id": 1,

"name": "Piyush Kamani",

"email": "[email protected]"

}

# Dictionary convert to json

jsonDictionary = json.dumps(myDictionary)

print(jsonDictionary)

Output:

'{"id": 1, "name": "Piyush Kamani", "email": "[email protected]"}'

Example 2:

main.py

# Created New Dictionary

myDictionary = {

"id": 1,

"name": "Piyush Kamani",

"email": "[email protected]"

}

# Dictionary convert to json file

with open("demo.json", "w") as outfile:

json.dump(myDictionary, outfile)

print("Created new demo.json file")

Output:

Created new demo.json file

#Python

鉁岋笍 Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3